#pragma once
#include <opencv2/opencv.hpp>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <ostream>
#include <vector>

#include <cstring>
#include <fstream>
#include <numeric>

#include "clipper.h"
#include "rockchip/rkmodel_struct.h"

using namespace std;

namespace ai {

void Stringsplit(const string& str, const string& split, vector<string>& res);
void numarrbitcmp(int* arr,int* arr2,int len,bool* &res);
void numandarrbitcmp(int* arr,int val,int len,bool* &res);
void arrbit_and_op(bool* arr,bool* arr2,int len,bool* &res);

void PlateReco_baidu_model(std::vector<float>&,std::vector<float>&, int* , float* );
void Plate_wordpos_det_baidu_model(cv::Mat ,int dest_width,int dest_height,std::vector<std::vector<cv::Point>>&);


class OCRPostProcessor {
public:
  void GetContourArea(const std::vector<std::vector<float>> &box,
                      float unclip_ratio, float &distance);

  cv::RotatedRect UnClip(std::vector<std::vector<float>> box,
                         const float &unclip_ratio);

  float **Mat2Vec(cv::Mat mat);

  int OrderPointsClockwise(std::vector<cv::Point> &pts);

  std::vector<std::vector<float>> GetMiniBoxes(cv::RotatedRect box,
                                               float &ssid);

  float BoxScoreFast(std::vector<std::vector<float>> box_array, cv::Mat pred);


  int BoxesFromBitmap(std::vector < TextDetInfo > &text_lines, 
                  const cv::Mat pred, const cv::Mat bitmap,
                  const float &box_thresh, 
                  const float &det_db_unclip_ratio,
                  const float &det_expand_by_min_score);

  int FilterTagDetRes(std::vector < TextDetInfo > &text_lines,
                  float ratio_h, float ratio_w, cv::Mat srcimg);

  int unclip_for_points(std::vector<cv::Point> &contour, 
                                    const float &det_db_unclip_ratio,
                                    const float &box_thresh,
                                    const cv::Mat &bitmap,
                                    const cv::Mat &pred,
                                    std::vector<cv::Point> &four_points,
                                    float &score,
                                    bool extra_mode = false);

private:
  static bool XsortInt(std::vector<int> a, std::vector<int> b);

  static bool XsortFp32(std::vector<float> a, std::vector<float> b);

  std::vector<std::vector<float>> Mat2Vector(cv::Mat mat);

  inline int _max(int a, int b) { return a >= b ? a : b; }

  inline int _min(int a, int b) { return a >= b ? b : a; }

  template <class T> inline T clamp(T x, T min, T max) {
    if (x > max)
      return max;
    if (x < min)
      return min;
    return x;
  }

  inline float clampf(float x, float min, float max) {
    if (x > max)
      return max;
    if (x < min)
      return min;
    return x;
  }
};

} // namespace paddle_infer

